<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/17
 * Time: 8:37
 */

namespace app\admin\controller;

use app\admin\model\Activity as ActivityModel;
use app\admin\model\CouponCategory as CouponCategoryModel;
use think\Config;

class ActivityController extends BaseController
{
    //is_have_coupon        是否有优惠券 0(无) 1(满减) 2(折扣)
    //is_ongoing            活动是否正在进行  0(活动已关闭) 1(活动进行中)
    //is_home_page          首页显示 0(首页不显示)  1(首页显示)
    //activity_type         活动类型 0(新人专享)    1(非新人专享)
    public function index()
    {
        $activities = ActivityModel::order(['id'=>'asc'])->paginate(15,false,['type'=>'page\Page']);
        foreach($activities as $k=>$v){
            $coupons = [];
            $coupons = explode(',',$v['coupon_ids']);
            $v['child'] = CouponCategoryModel::where('id','in',$coupons)->select();
            $acti[] = $v;
        }
        if(empty($acti)){
            $acti = [];
        }
        $this->assign([
            'activityType' => Config::get('setting.activity_type'),
            'acti' => $acti,
            'coupon' => Config::get('setting.tickets_category'),
            'activities' => $activities,
        ]);
        return $this->fetch();
    }

    public function add()
    {
        if($this->request->isGet()){
            $coupon = Config::get('setting.tickets_category');
            foreach($coupon as $k=>$v){
                $coupon[$k]['child'] = CouponCategoryModel::where(['coupon_cate_cate'=>$v['id']])->select();
            }
            $this->assign([
                'activityType' => Config::get('setting.activity_type'),
                'couponCategory' => $coupon,
            ]);
            return $this->fetch();
        }
        if($this->request->isPost()){
            $params = $this->request->param();
            if($this->request->file('file-2')){
                $uploadFile = uploadFile('file-2',null,null,'uploads/activity');
                $params['head_img'] = $uploadFile['origin'];
                $params['head_img_origin'] = $uploadFile['tag'];
            }

            if(!empty($params['coupon_ids'])){
                $params['coupon_ids'] = implode(',',$params['coupon_ids']);
            }
            if($params['activity_type'] == 0) {
                $memberActivity = $this->checkNewMemberActivity();
                if ($memberActivity) {
                    ActivityModel::where('id', 'eq', $memberActivity)->update(['is_ongoing' => 0]);
                }
            }
            if($params['is_home_page'] == 1){
                $homePageActivity = $this->checkHomePageActivity();
                if($homePageActivity){
                    ActivityModel::where('id','eq',$homePageActivity)->update(['is_ongoing'=>0]);
                }
            }
            $model = new ActivityModel();
            if($model->allowField(true)->isUpdate(false)->save($params)){
                return ['error'=>0,'msg'=>'操作成功!'];
            }else{
                return ['error'=>1,'msg'=>'操作失败!'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作'];
    }

    public function edi()
    {
        if($this->request->isGet()){
            $coupon = Config::get('setting.tickets_category');
            foreach($coupon as $k=>$v){
                $coupon[$k]['child'] = CouponCategoryModel::where(['coupon_cate_cate'=>$v['id']])->select();
            }

            $id = $this->request->param('id');
            $this->assign([
                'activityType' => Config::get('setting.activity_type'),
                'couponCategory' => $coupon,
                'activity' => ActivityModel::find($id),
            ]);
            return $this->fetch();
        }
        if($this->request->isPost()){
            $params = $this->request->param();
            if($this->request->file('file-2')){
                $uploadFile = uploadFile('file-2',null,null,'uploads/activity');
                $params['head_img'] = $uploadFile['origin'];
                $params['head_img_origin'] = $uploadFile['tag'];
                $oldFile = ActivityModel::field('head_img,head_img_origin')->find($params['id']);
                uploadDeleteFile([[$oldFile->head_img,$oldFile->head_img_origin]]);
            }
            if(!empty($params['coupon_ids'])){
                $params['coupon_ids'] = implode(',',$params['coupon_ids']);
            }

            //is_have_coupon        是否有优惠券 0(无) 1(满减) 2(折扣)
            //is_ongoing            活动是否正在进行  0(活动已关闭) 1(活动进行中)
            //is_home_page          首页显示 0(首页不显示)  1(首页显示)
            //activity_type         活动类型 0(新人专享)    1(非新人专享)
            if((!empty($params['activity_type'])) && ($params['activity_type'] == 0)){
                $memberActivity = $this->checkNewMemberActivity();
                if($memberActivity && ($memberActivity != $params['id'])){
                    ActivityModel::where('id','eq',$memberActivity)->update(['is_ongoing'=>0]);
                }
            }

            if($params['is_home_page'] == 1){
                $homePageActivity = $this->checkHomePageActivity();
                if($homePageActivity && ($homePageActivity != $params['id'])){
                    ActivityModel::where('id','eq',$homePageActivity)->update(['is_ongoing'=>0]);
                }
            }

            $model = new ActivityModel();
            if($model->allowField(true)->isUpdate(true)->save($params)){
                return ['error'=>0,'msg'=>'操作成功!'];
            }else{
                return ['error'=>1,'msg'=>'操作失败!'];
            }
        }
        return ['error'=>9,'msg'=>'非法操作'];
    }

    public function del()
    {
        $id = $this->request->param('id');
        $oldFile = ActivityModel::field('head_img,head_img_origin')->find($id);
        uploadDeleteFile([[$oldFile->head_img,$oldFile->head_img_origin]]);
        if(ActivityModel::where('id','eq',$id)->delete()){
            return ['error'=>0,'操作成功!'];
        }else{
            return ['error'=>1,'操作失败!'];
        }
    }

    //修改活动状态
    public function changActivity()
    {
        $params = $this->request->param();
        if(ActivityModel::where(['id'=>$params['id']])->update(['is_ongoing'=>$params['is_ongoing']])){
            return ['error'=>0,'msg'=>'操作成功'];
        }else{
            return ['error'=>1,'msg'=>'操作成功'];
        }
    }

    //点击活动标题限时活动详情
    public function detail()
    {
        $id = $this->request->param('id');
        $this->assign([
           'detail' => ActivityModel::field('title,details')->find($id),
        ]);
        return $this->fetch();
    }

    //设定新人专享活动只可能有一个,若是重新添加新人专享活动或是打开一个心新的活动则默认关闭当前正在举行的新人专享活动
    public function checkNewMemberActivity()
    {


        //is_have_coupon        是否有优惠券 0(无) 1(满减) 2(折扣)
        //is_ongoing            活动是否正在进行  0(活动已关闭) 1(活动进行中)
        //is_home_page          首页显示 0(首页不显示)  1(首页显示)
        //activity_type         活动类型 0(新人专享)    1(非新人专享)
        $memberActivity =  ActivityModel::where(['activity_type'=>0,'is_ongoing'=>1])->find();
        if($memberActivity){
            return $memberActivity->id;
        }else{
            return false;
        }
    }

    public function checkHomePageActivity()
    {
        //is_have_coupon        是否有优惠券 0(无) 1(满减) 2(折扣)
        //is_ongoing            活动是否正在进行  0(活动已关闭) 1(活动进行中)
        //is_home_page          首页显示 0(首页不显示)  1(首页显示)
        //activity_type         活动类型 0(新人专享)    1(非新人专享)
        $homePageActivity =  ActivityModel::where(['is_home_page'=>1,'is_ongoing'=>1])->find();
        if($homePageActivity){
            return $homePageActivity->id;
        }else{
            return false;
        }
    }

}